/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ac.engin.treasury;

import ac.settings.dataModel;
import ac.settings.files;
import ac.settings.sqlQuerys;
import ac.uitls.propertiesOperation;
import java.util.Date;

/**
 *
 * @author ismail
 */
public class treasuryData extends ac.ui.FrameOperationTemplat implements dataModel, sqlQuerys, files {

    String Table = TABLE_treasury_Balance;

    String actionField = "treasury_id";
    String meTabel = TABLE_treasury_movies;
    propertiesOperation po = new propertiesOperation();

    public String getlastID() {
        String[] r;
        r = db.getRow(meTabel, " max(id) as m ", "");
        return r[0];

    }

    private int t(String id, double data, String Status) {
        Double newB = 0d;
        Double oldB = 0d;

        String[] oldb_data = db.getRow(Table, "*", actionField + "='" + id + "'");

        if (oldb_data[2] != null && !oldb_data[2].equalsIgnoreCase("null")) {
            oldB = Double.parseDouble(oldb_data[2]);
        }

        if (Status.equals("-")) {

            newB = oldB - data;
        } else {
            newB = oldB + data;
        }

        if (oldb_data[2] != null && !oldb_data[2].equalsIgnoreCase("null")) {
            String[][] usedata = {new String[]{"balance"}, new String[]{"" + newB}};
            db.setUpdate(Table, usedata, actionField + "='" + id + "'");
        } else {

            String[][] usedata = {new String[]{actionField, "balance"}, new String[]{id, "" + newB}};
            db.setInsert(Table, usedata);

        }
        ac.GUI.index.updateTinfo(id, true);
        return db.retuenstatus;
    }

    public String[] getRow(String id) {
        String[] r;
        r = db.getRow(meTabel, "*", "id='" + id + "'");
        return r;

    }

    public String returnMoevType(String id) {

        return getRow(id)[1];

    }

    public int AddToTreasury(String id, double data) {
        int returnstatus = 0;

        returnstatus = t(id, data, "+");

        return returnstatus;

    }

    public void removeTreasury(String id) {
        db.setDelete(Table, actionField + "='" + id + "'");

    }

    public int CutToTreasury(String id, double data) {
        int returnstatus = 0;

        returnstatus = t(id, data, "-");

        return returnstatus;
    }

    public double getbalance(String id) {

        double d = 0d;

        String[] oldb_data = db.getRow(Table, "*", actionField + "='" + id + "'");

        if (oldb_data[2] != null) {

            d = Double.parseDouble(oldb_data[2]);
        }

        return d;

    }

    public void reportTreasuryStatus() {
        //SQL_treasury 
   TabelString = meTabel;
        setPrint(REPORT_treasury_status, SQL_treasury);

    }

    public void reportTreasuryMovesStatus(String where, String[] updateTitles) {
        //SQL_treasury 

        TabelString = meTabel;
        ac.reportes.reportes.updateReporteTitles(updateTitles[0],
                updateTitles[1],
                updateTitles[2],
                updateTitles[3],
                updateTitles[4]);

        setPrint(REPORT_treasury_Movies_status, SQL_treasury_Movies_All + where);

    }

    public void reportTreasurWStatus(String tid, String oid, Date d1, Date d2) {
        //SQL_treasury 

        String[] reptitles = new String[5];

        String where = " where treasury_movies.`type`='w'";

        if (tid != null && !tid.equals("")) {
            String T = db.getRow(tableAddstree, "*", Integer.parseInt(tid))[1];
            reptitles[3] = po.getWord("label.treasury") + " : " + T;

            where += " and treasury_movies.treasury_id='" + tid + "'";

        } else {

            reptitles[3] = "-";

        }

        if (oid != null && !oid.equals("")) {
            String T = db.getRow(tableAddstree, "*", Integer.parseInt(oid))[1];
            reptitles[4] = po.getWord("label.offcial") + " : " + T;

            where += " and treasury_movies.offcial='" + oid + "'";

        } else {

            reptitles[4] = "-";

        }

        if (d1 != null && d2 != null) {
            where += " and   " + ac.lib.data.dateTime.updateSerechDates(meTabel + ".date", d1, d2);
            reptitles[2] = po.getWord("label.from") + " " + d1.toString() + " " + po.getWord("label.to") + " " + d2.toString();
        } else {
            reptitles[2] = "-";
        }

        reptitles[0] = po.getWord("title.reportTreasurWStatus");

        reptitles[1] = po.getWord("label.type") + " : " + po.getWord("label.withdraw");

        reportTreasuryMovesStatus(where, reptitles);

    }

    public void reportTreasurDStatus(String tid, String oid, Date d1, Date d2) {
        //SQL_treasury 

        String[] reptitles = new String[5];

        String where = " where treasury_movies.`type`='d'";

        if (tid != null && !tid.equals("")) {
            String T = db.getRow(tableAddstree, "*", Integer.parseInt(tid))[1];
            reptitles[3] = po.getWord("label.treasury") + " : " + T;

            where += " and treasury_movies.treasury_id='" + tid + "'";

        } else {

            reptitles[3] = "-";

        }

        if (oid != null && !oid.equals("")) {
            String T = db.getRow(tableAddstree, "*", Integer.parseInt(oid))[1];
            reptitles[4] = po.getWord("label.offcial") + " : " + T;

            where += " and treasury_movies.offcial='" + oid + "'";

        } else {

            reptitles[4] = "-";

        }

        if (d1 != null && d2 != null) {
            where += " and   " + ac.lib.data.dateTime.updateSerechDates(meTabel + ".date", d1, d2);
            reptitles[2] = po.getWord("label.from") + " " + d1.toString() + " " + po.getWord("label.to") + " " + d2.toString();
        } else {
            reptitles[2] = "-";
        }

        reptitles[0] = po.getWord("title.reportTreasurDStatus");

        reptitles[1] = po.getWord("label.type") + " : " + po.getWord("label.Deposit");

        reportTreasuryMovesStatus(where, reptitles);

    }
}
